package com.wordplat.quickstart.xutils.view;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Component;

/**
 * Author: wyouflf
 * Date: 13-9-9
 * Time: 下午12:29
 *
 * @since 2021-05-09
 */
final class ViewFinder {
    private Component view;
    private AbilitySlice activity;

    /**
     * ViewFinder
     *
     * @param view
     */
    public ViewFinder(Component view) {
        this.view = view;
    }

    /**
     * ViewFinder
     *
     * @param activity
     */
    public ViewFinder(AbilitySlice activity) {
        this.activity = activity;
    }

    /**
     * findViewById
     *
     * @param id
     * @return Component
     */
    public Component findViewById(int id) {
        if (view != null) {
            return view.findComponentById(id);
        }
        if (activity != null) {
            return activity.findComponentById(id);
        }
        return null;
    }

    /**
     * findViewByInfo
     *
     * @param info
     * @return Component
     */
    public Component findViewByInfo(ViewInfo info) {
        return findViewById(info.value, info.parentId);
    }

    /**
     * findViewById
     *
     * @param id
     * @param pid
     * @return Component
     */
    public Component findViewById(int id, int pid) {
        Component pView = null;
        if (pid > 0) {
            pView = this.findViewById(pid);
        }

        Component view = null;
        if (pView != null) {
            view = pView.findComponentById(id);
        } else {
            view = this.findViewById(id);
        }
        return view;
    }
}
